{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d0cb671e5678dac1",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "学习目标\n",
    "- 应用数组的通用判断函数\n",
    "- 应用np.where实现数组的三元运算"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3c0f6ab8b787ebf5",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 1 逻辑运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3c587b3f3feca1bc",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.801156700Z",
     "start_time": "2024-02-20T10:56:35.732314300Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[95, 89, 88, 66, 73],\n",
       "       [75, 55, 99, 59, 74],\n",
       "       [80, 51, 59, 89, 84],\n",
       "       [80, 96, 92, 58, 97],\n",
       "       [54, 70, 93, 67, 59],\n",
       "       [48, 44, 43, 94, 63],\n",
       "       [41, 44, 97, 49, 75],\n",
       "       [63, 64, 98, 54, 43],\n",
       "       [70, 70, 51, 92, 95],\n",
       "       [77, 57, 54, 90, 79]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 生成10名同学，5门功课的数据\n",
    "score = np.random.randint(40, 100, (10, 5))\n",
    "score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "870434f4bea53560",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.818107200Z",
     "start_time": "2024-02-20T10:56:35.803122500Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[41, 44, 97, 49, 75],\n",
       "       [63, 64, 98, 54, 43],\n",
       "       [70, 70, 51, 92, 95],\n",
       "       [77, 57, 54, 90, 79]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 取出最后4名同学的成绩，用于逻辑判断\n",
    "test_score = score[6:, 0:5]\n",
    "test_score"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "d73a2d85d33171e3",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.827428800Z",
     "start_time": "2024-02-20T10:56:35.820103900Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[False, False,  True, False,  True],\n",
       "       [ True,  True,  True, False, False],\n",
       "       [ True,  True, False,  True,  True],\n",
       "       [ True, False, False,  True,  True]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_score > 60"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3d0e3aa21df3daa6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.859806700Z",
     "start_time": "2024-02-20T10:56:35.824429600Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[41, 44,  1, 49,  1],\n",
       "       [ 1,  1,  1, 54, 43],\n",
       "       [ 1,  1, 51,  1,  1],\n",
       "       [ 1, 57, 54,  1,  1]])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# BOOL赋值, 将满足条件的设置为指定的值-布尔索引\n",
    "test_score[test_score > 60] = 1\n",
    "test_score"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13dbe5224e1bfb47",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 2 通用判断函数\n",
    "- np.all()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "52bc29449291841b",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.859806700Z",
     "start_time": "2024-02-20T10:56:35.833492300Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前两名同学的成绩[0:2, :]是否全及格\n",
    "np.all(score[0:2, :] > 60)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bbb6bd0b9fe7ea5a",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "- np.any()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "bbd9e15b9e5267e4",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.859806700Z",
     "start_time": "2024-02-20T10:56:35.839928200Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前两名同学的成绩[0:2, :]是否有大于90分的\n",
    "np.any(score[0:2, :] > 80)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6f12abfe1e6a5b0f",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 3 np.where(三元运算符)\n",
    "通过使用np.where能够进行更加复杂的运算\n",
    "- np.where()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "cc61d9033d25f1c0",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.860805100Z",
     "start_time": "2024-02-20T10:56:35.846612800Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1],\n",
       "       [1, 0, 1, 0],\n",
       "       [1, 0, 0, 1],\n",
       "       [1, 1, 1, 0]])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前四名学生,前四门课程中，成绩中大于60的置为1，否则为0\n",
    "temp = score[:4, :4]\n",
    "np.where(temp > 60, 1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "475f247cc5f44c00",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "- 复合逻辑需要结合np.logical_and和np.logical_or使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "62385ae626e40c7d",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.893863700Z",
     "start_time": "2024-02-20T10:56:35.853429800Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0, 1, 1, 1],\n",
       "       [1, 0, 0, 0],\n",
       "       [1, 0, 0, 1],\n",
       "       [1, 0, 0, 0]])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前四名学生,前四门课程中，成绩中大于60且小于90的换为1，否则为0\n",
    "np.where(np.logical_and(temp > 60, temp < 90), 1, 0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "5ee29ff797d97363",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.895862600Z",
     "start_time": "2024-02-20T10:56:35.860805100Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0, 0, 0],\n",
       "       [0, 1, 1, 1],\n",
       "       [0, 1, 1, 0],\n",
       "       [0, 1, 1, 1]])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 判断前四名学生,前四门课程中，成绩中大于90或小于60的换为1，否则为0\n",
    "np.where(np.logical_or(temp > 90, temp < 60), 1, 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1663dc9092e101cd",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 4 统计运算\n",
    "## 4.1 统计指标\n",
    "在数据挖掘/机器学习领域，统计指标的值也是我们分析问题的一种方式。常用的指标如下：\n",
    "- **min(a, axis)**\n",
    "    - 返回数组或沿指定轴的最小值。\n",
    "    \n",
    "- **max(a, axis)**\n",
    "    - 返回数组或沿指定轴的最大值。\n",
    "    \n",
    "- **median(a, axis)**\n",
    "    - 计算沿指定轴的中位数。\n",
    "    \n",
    "- **mean(a, axis, dtype)**\n",
    "    - 计算沿指定轴的算术平均值。\n",
    "    \n",
    "- **std(a, axis, dtype)**\n",
    "    - 计算沿指定轴的标准差。\n",
    "    \n",
    "- **var(a, axis, dtype)**\n",
    "    - 计算沿指定轴的方差。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d11fbbfa3e462613",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "## 4.2 案例：学生成绩统计运算\n",
    "进行统计的时候，axis 轴的取值并不一定，Numpy中不同的API轴的值都不一样，在这里，axis 0代表列, axis 1代表行去进行统计"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "7d3adcfddbe54fdd",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.916834800Z",
     "start_time": "2024-02-20T10:56:35.898863200Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前四名学生,各科成绩的最大分：[95 96 99 89 97]\n",
      "前四名学生,各科成绩的最小分：[75 51 59 58 73]\n",
      "前四名学生,各科成绩波动情况：[ 7.5        19.95463605 15.23975065 12.509996    9.6695398 ]\n",
      "前四名学生,各科成绩的平均分：[82.5  72.75 84.5  68.   82.  ]\n"
     ]
    }
   ],
   "source": [
    "# 接下来对于前四名学生,进行一些统计运算\n",
    "# 指定列 去统计\n",
    "temp = score[:4, 0:5]\n",
    "print(\"前四名学生,各科成绩的最大分：{}\".format(np.max(temp, axis=0)))\n",
    "print(\"前四名学生,各科成绩的最小分：{}\".format(np.min(temp, axis=0)))\n",
    "print(\"前四名学生,各科成绩波动情况：{}\".format(np.std(temp, axis=0)))\n",
    "print(\"前四名学生,各科成绩的平均分：{}\".format(np.mean(temp, axis=0)))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "766989ad6592e45b",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "如果需要统计出某科最高分对应的是哪个同学？\n",
    "这两个函数是 NumPy 中用于找到数组中最大值和最小值的索引的函数。\n",
    "- np.argmax(temp, axis=)\n",
    "- np.argmin(temp, axis=)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "9071394189d1b25",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.958931100Z",
     "start_time": "2024-02-20T10:56:35.919833400Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "前四名学生，各科成绩最高分对应的学生下标：[0 3 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "print(\"前四名学生，各科成绩最高分对应的学生下标：{}\".format(np.argmax(temp, axis=0)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "f9a9fc1c3962c6d6",
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-02-20T10:56:35.959933800Z",
     "start_time": "2024-02-20T10:56:35.923291500Z"
    },
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[95, 89, 88, 66, 73],\n",
       "       [75, 55, 99, 59, 74],\n",
       "       [80, 51, 59, 89, 84],\n",
       "       [80, 96, 92, 58, 97]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "temp"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ef92e284c154cb1",
   "metadata": {
    "collapsed": false,
    "jupyter": {
     "outputs_hidden": false
    }
   },
   "source": [
    "# 5 小结\n",
    "- 逻辑运算【知道】\n",
    "    - 直接进行大于,小于的判断\n",
    "    - 合适之后,可以直接进行赋值\n",
    "- 通用判断函数【知道】\n",
    "    - np.all()\n",
    "    - np.any()\n",
    "- 统计运算【掌握】\n",
    "    - np.max()\n",
    "    - np.min()\n",
    "    - np.median()\n",
    "    - np.mean()\n",
    "    - np.std()\n",
    "    - np.var()\n",
    "    - np.argmax(axis=) — 最大元素对应的下标\n",
    "    - np.argmin(axis=) — 最小元素对应的下标"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
